package whf.framework.web.tag;

import javax.servlet.jsp.JspException;

import whf.framework.log.Log;
import whf.framework.log.LogFactory;
import whf.framework.web.tag.ext.TagConstants;
import whf.framework.web.tag.ext.TagStructureException;


/**
 * @author wanghaifeng
 * 
 */
public class TableCaptionTag extends HtmlTag {
	private static final long serialVersionUID = 899149338534L;

	static Log log = LogFactory.getLog(TableCaptionTag.class);

	public String getOpenTag() {
		return super.getOpenTag(TagConstants.TAGNAME_CAPTION);
	}

	public String getCloseTag() {
		return super.getCloseTag(TagConstants.TAGNAME_CAPTION);
	}

	public int doStartTag() throws JspException {
		TableTag tableTag = (TableTag) findAncestorWithClass(this,
				TableTag.class);
		if (tableTag == null) {
			throw new TagStructureException(getClass(), "caption", "table");
		}
		if (tableTag.isFirstIteration()) {
			return 2;
		}

		return SKIP_BODY;
	}

	public int doEndTag() throws JspException {
		TableTag tableTag = (TableTag) findAncestorWithClass(this,
				TableTag.class);
		if (tableTag == null) {
			throw new TagStructureException(getClass(), "caption", "table");
		}
		if(tableTag.isFirstIteration()){
			if (getBodyContent() != null) {
				tableTag.setCaption(getBodyContent().getString());
			}
		}
		this.release();
		return super.doEndTag();
	}

	public void release() {
		super.release();
	}

}